package cn.jdy.plugins.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.ExamOptions;

/**
 * @desc   试题选项实体操作
 * @author ningqian
 * @date   2016-7-5
 */
@Service
public class ExamOptionsService extends BaseDao<ExamOptions>{

	/**
	 * 按条件查询
	 * @param examOptions
	 * @return
	 */
	public List<ExamOptions> query(ExamOptions examOptions, String... date) {
		if(null == examOptions) return super.findAll();
		if(examOptions.getiDisplayLength() > 0){
		  return super.find(createFilter(examOptions, date),examOptions.getiDisplayStart(),examOptions.getiDisplayLength());
		}
		return super.find(createFilter(examOptions, date));
	}
	
	/**
	 * 获取选项
	 * @param questionId
	 * @return
	 */
	public List<ExamOptions> query(Long questionId) {
		List<ExamOptions> list = new ArrayList<ExamOptions>();
		if(null != questionId) {
			list = super.find(createFilterS(questionId));
		}
		return list;
	}
	
	public Long count(ExamOptions examOptions, String... date) {
		return super.count(createFilter(examOptions, date));
	}
	
	public Long count(ExamOptions examOptions){
		return super.count(createFilter(examOptions));
	}
	
	/**
	 * 拼装条件(所属题干、选项名、创建时间)
	 * @param evaluationProject
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(ExamOptions examOptions, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != examOptions.getQuestionId()){
		  dc.add(Restrictions.eq("questionId", examOptions.getQuestionId()));
	  }
	  
	  if(!StringUtil.isNullOrEmpty(examOptions.getOptionName())){
		  dc.add(Restrictions.like("optionName", examOptions.getOptionName().trim(), MatchMode.ANYWHERE));
	  }
	  
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("insertTime", date[0],date[1]));
	  }
	  
	  dc.addOrder(Order.asc("sort"));
		return dc;
	}


	/**
	 * 拼装条件(所属题干)
	 * @param examOptions
	 * @return
	 */
	private DetachedCriteria createFilter(ExamOptions examOptions) {
	 DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != examOptions.getQuestionId()){
		  dc.add(Restrictions.eq("questionId", examOptions.getQuestionId()));
	  }
	  
	  return dc;
	}
	
	/**
	 * 拼装条件(所属题干)
	 * @param questionId
	 * @return
	 */
	private DetachedCriteria createFilterS(Long questionId) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != questionId){
		  dc.add(Restrictions.eq("questionId", questionId));
	  }
	  
	  dc.addOrder(Order.desc("sort"));
		return dc;
	}
	
	/**
	 * 获取试题sort最大值
	 * @return maxSort
	 */
	public Integer getMaxSort(Long questionId){
		List<ExamOptions> list = new ArrayList<ExamOptions>();
		int maxSort=0;
		try {
			list = super.find(createFilterS(questionId));
			if(list.size()>0){
				maxSort=list.get(0).getSort();
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return maxSort;
	}
	
	
}
